import 'dart:io';
import 'show_dir.dart';
import 'package:flutter/material.dart';
import 'package:my_app/src/rust/frb_generated.dart';

import 'package:path_provider/path_provider.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path/path.dart' as path;
import 'package:get/get.dart';

Future<void> main() async {
  await RustLib.init();
  runApp(GetMaterialApp(home: MyApp()));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: const Text("杰微电子 - 孔祥旭Demo"),
          centerTitle: true,
          backgroundColor: Colors.lightBlue,
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                onPressed: () async {
                  // 初始化相机对象
                  final ImagePicker picker = ImagePicker();

                  // 打开相机, 进行拍照
                  // /data/user/0/com.example.demo/cache/e3935a18-41d7-462a-b1ba-8cd57751477f6936435358296295777.jpg
                  final XFile? photo =
                      await picker.pickImage(source: ImageSource.camera);

                  // 获取应用的文档目录 /data/user/0/com.example.demo/app_flutter
                  final directory = await getApplicationDocumentsDirectory();

                  final String newPath =
                      path.join(directory.path, path.basename(photo!.name));
                  final File newImage = await File(photo.path).copy(newPath);

                  print(newImage);

                  print("图片保存完毕");
                },
                child: const Text("上传图片"),
              ),
              ElevatedButton(
                  onPressed: () async {
                    final directory = await getApplicationDocumentsDirectory();

                    Get.to(ShowDirWidget(currentPath: directory.path));
                  },
                  child: const Text("遍历app目录"))
            ],
          ),
        ),
      ),
    );
  }
}
